<?php
class finalVar{
    public static $_NUMBER = 'N';
    public static $_ALPHANUMERIC = 'AN';
    public static $_UNUSED = 'UNU';
    public static $_ALIGNRIGHT = 'RIGHT';
    public static $_ALIGNLEFT = 'LEFT';
    public static $_ZERO = '0';
    public static $_BLANK = ' ';
}

/*
Field object for text file
*/
class field{
    private $type = '';
    private $value = '';
    private $leng = '';
    private $pos = '';

    public function setType($type){
        $this->type = $type;
    }

    public function getType(){
        return $this->type;
    }

    public function setValue($value){
        $this->value = $value;
    }

    public function getValue(){
        return $this->value;
    }

    public function setLeng($leng){
        $this->leng = $leng;
    }

    public function getLeng(){
        return $this->leng;
    }

    public function setPos($pos){
        $this->pos = $pos;
    }

    public function getPos(){
        return $this->pos;
    }

    public function __toString(){
        $_str = '';

        if ($this->type == finalVar::$_NUMBER){
            // number is right justified
            for ($v=0; $v<($this->leng)-(strlen($this->value)); $v++){
                $_str .= finalVar::$_ZERO;
            }
           $_str .= $this->value;
        }
        else if ($this->type == finalVar::$_ALPHANUMERIC) {
            // alphanumeric is left justified
            $_str .= $this->value;
            for ($v=0; $v<($this->leng)-(strlen($this->value)); $v++){
                $_str .= finalVar::$_BLANK;
            }
        }

        return $_str;
    }
}

class line{
    private static $_length = 152;
    private $fields = array();

    public function addField($field){
        array_push($this->fields,$field);
    }

    public function getFields(){
        return $this->fields;
    }

    public function isLengthValid(){
        $len = 0;
        for ($i=0; $i<count($this->fields); $i++){
            $len += strlen($this->fields[$i]);
        }

        if ($len != line::$_length){
            return false;
        }
        else {
            return true;
        }
    }

    public function __toString(){
        $str = '';

        for ($i=0; $i<count($this->fields); $i++){
            $str .= $this->fields[$i];
        }

        return $str.PHP_EOL;
    }
}
?>

